package learn3ack

import com.rabbitmq.client.CancelCallback
import com.rabbitmq.client.DeliverCallback
import com.rabbitmq.client.Delivery
import utils.RabbitMqUtils
import utils.SleepUtils

/**

 @author HSH

 @create 2022-01-06 10:33
  消息在手动应答时是不丢失放回队列中重新消费
 */
class Worker02ack {
    private static final String QUEUE_NAME = "ack_queue_d"

    static void main(String[] args) {
        def channel = RabbitMqUtils.getChannel()
        println "Worker02ack等待接收消息。。。。"
        println "时间较短的消费者。。。。"
        /***
         * 不公平分发设置
         */
//        channel.basicQos(1)
        /**
         * 预取值 2
         */
        channel.basicQos(2)
        channel.basicConsume(QUEUE_NAME,false,new DeliverCallback(){
            @Override
            void handle(String consumerTag, Delivery message) throws IOException {
               SleepUtils.sleep(1)
                println "接收到的消息是${new String(message.getBody(),"UTF-8")}"
                channel.basicAck(message.getEnvelope().getDeliveryTag(),false)
            }
        },new CancelCallback(){

            @Override
            void handle(String consumerTag) throws IOException {
                println "${consumerTag}消息消息消费被中断"
            }
        })
    }
}
